package cn.lzwei.bilimall.product.service.impl;

import com.mysql.cj.util.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.lzwei.common.utils.PageUtils;
import cn.lzwei.common.utils.Query;

import cn.lzwei.bilimall.product.dao.SkuInfoDao;
import cn.lzwei.bilimall.product.entity.SkuInfoEntity;
import cn.lzwei.bilimall.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }
    /**
     * 检索sku：模糊查询、分类、品牌、价格区间
     */
    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
        //1.模糊查询
        String key = (String) params.get("key");
        if(!StringUtils.isNullOrEmpty(key)){
            queryWrapper.and(item->{
                item.eq("sku_id",key).or().like("sku_name",key);
            });
        }
        //2.分类
        String catelogId = (String) params.get("catelogId");
        if(!StringUtils.isNullOrEmpty(catelogId) && !"0".equals(catelogId)){
            queryWrapper.eq("catalog_id",catelogId);
        }
        //3.品牌
        String brandId = (String) params.get("brandId");
        if(!StringUtils.isNullOrEmpty(brandId) && !"0".equals(brandId)){
            queryWrapper.eq("brand_id",brandId);
        }
        //4.价格区间
        String min = (String) params.get("min");
        if(!StringUtils.isNullOrEmpty(min)){
            queryWrapper.ge("price",min);
        }
        String max = (String) params.get("max");
        if(!StringUtils.isNullOrEmpty(max)){
            int value = new BigDecimal(max).compareTo(BigDecimal.valueOf(0));
            if (value>0){
                queryWrapper.le("price",max);
            }
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

}